var app = getApp()
var that;
var latitude = '';
var longitude = '';
Page({
  data: {
    location: {
      longitude: "",
      latitude: ""
    },
    markers: []
    
  },
  onLoad: function (options) {
    // this.getLocation()
    this.getCurrentLocation()
  },
  //获取经纬度
  getLocation: function (e) {
    console.log(e)
    that = this
    wx.getLocation({
      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标  
      success: function (res) {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          location: {
            longitude: res.longitude,
            latitude: res.latitude
          }
        })
        that.openLocation()
      }
    })
  },
  //根据经纬度在地图上显示
  openLocation: function () {
    // var value = e.detail.value
    wx.openLocation({
      longitude: Number(that.data.longitude),
      latitude: Number(that.data.latitude)
    })
  },
  getCurrentLocation: function () {
    that = this
    wx.request({
      url: 'https://pi-home.cn/mini-weixin/gps/getCurrentGps', 
      header: {
        'content-type': 'application/json'
      },
      method: "POST",
      success: function (res) {
        that.setData({
          latitude: res.data.lat,
          longitude: res.data.lon,
          location: {
            longitude: res.data.lon,
            latitude: res.data.lat
          },
          markers: [{
            iconPath: "../../img/marker_red.png",
            id: 0,
            longitude: res.data.lon,
            latitude: res.data.lat,
            width: 25,
            height: 25
          }
          ]
           
        })
        // that.openLocation()
      }
    })
  }
})  